package com.tcs.test.executor.base;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

import org.junit.After;
import org.junit.Before;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class BeseExecutorRun {

	private final Logger logger = LoggerFactory.getLogger(BeseExecutorRun.class);
	
	protected final ReentrantLock mainLock = new ReentrantLock();
	
	protected final Condition condition = mainLock.newCondition();
	
	protected long beginTime,endTime = 0;
	
	@Before
	public void begin() {
		beginTime = System.currentTimeMillis();
	}
	
	@After
	public void end() {
		endTime = System.currentTimeMillis();
		logger.info(" 用时 : {} 秒!!" , endTime - beginTime);
	}
	
	protected void go() throws InterruptedException {
		mainLock.lockInterruptibly();
		try {
			this.service();
			condition.await();
		} catch (Exception e) {
			logger.error("" , e);
		} finally {
			mainLock.unlock();
		}
	}
	
	protected abstract void service();
}
